package com.example.score;

import androidx.constraintlayout.solver.ArrayLinkedVariables;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.Stack;


public class MyViewModel extends ViewModel {
    private MutableLiveData<Integer> aTeamScore;
    private MutableLiveData<Integer> bTeamScore;
    private Stack<Integer> aBack = new Stack<>(), bBack = new Stack<>();

    public MutableLiveData<Integer> getaTeamScore() {
        if (aTeamScore == null) {
            aTeamScore = new MutableLiveData<>();
            aTeamScore.setValue(0);
        }
        return aTeamScore;
    }

    public MutableLiveData<Integer> getbTeamScore() {
        if (bTeamScore == null) {
            bTeamScore = new MutableLiveData<>();
            bTeamScore.setValue(0);
        }
        return bTeamScore;
    }

    //a队加分
    public void aTeamAdd(int p) {
        aBack.push(p);
        bBack.push(0);
        aTeamScore.setValue(p + aTeamScore.getValue());
    }

    //b队加分
    public void bTeamAdd(int p) {
        bBack.push(p);
        aBack.push(0);
        bTeamScore.setValue(p + bTeamScore.getValue());
    }

    //重置两队分数
    public void resetTeamScore() {
        aBack.push(aTeamScore.getValue());
        bBack.push(bTeamScore.getValue());
        aTeamScore.setValue(0);
        bTeamScore.setValue(0);
    }

    //撤销之前误操作
    public void reverseTeamScore() {
        if (!aBack.empty()) {
            if (aTeamScore.getValue() == 0 && bTeamScore.getValue() == 0) {
                aTeamScore.setValue(aBack.pop());
                bTeamScore.setValue(bBack.pop());
            } else {
                aTeamScore.setValue(aTeamScore.getValue() - aBack.pop());
                bTeamScore.setValue(bTeamScore.getValue() - bBack.pop());
            }
        }
    }

}
